package com.manage.system.svc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.manage.common.core.result.JsonResult;
import com.manage.common.core.utils.PageUtil;
import com.manage.system.api.dto.OauthClientDetailsDto;
import com.manage.system.api.entity.OauthClientDetails;
import com.manage.system.api.request.client.OauthClientParam;
import com.manage.system.api.service.IOauthClientDetailsService;
import com.manage.system.svc.convert.BeanCovert;
import com.manage.system.svc.mapper.OauthClientDetailsMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 系统部门表 服务实现类
 * </p>
 *
 * @author zhangqing
 * @since 2021-03-05
 */
@Service
@AllArgsConstructor
public class OauthClientDetailServiceImpl extends ServiceImpl<OauthClientDetailsMapper, OauthClientDetails> implements IOauthClientDetailsService {

    private final OauthClientDetailsMapper oauthClientDetailsMapper;

    @Override
    public JsonResult findListPage(OauthClientParam oauthClientParam) {
        LambdaQueryWrapper<OauthClientDetails> like = new QueryWrapper<OauthClientDetails>().lambda()
                .eq(!StringUtils.isEmpty(oauthClientParam.getClientId()), OauthClientDetails::getClientId, oauthClientParam.getClientId())
                .like(!StringUtils.isEmpty(oauthClientParam.getScope()), OauthClientDetails::getScope, oauthClientParam.getScope());

        PageInfo<OauthClientDetails> pageInfo = PageUtil.startPage(oauthClientParam.getPageNum(), oauthClientParam.getPageSize())
                .doSelectPageInfo(() -> oauthClientDetailsMapper.selectList(like));

        return JsonResult.success(PageUtil.restPage(pageInfo.getList()));
    }

    @Override
    public JsonResult updateOauthClientDetail(OauthClientDetailsDto oauthClientDetailsDto) {
        LambdaUpdateWrapper<OauthClientDetails> updateWrapper = new UpdateWrapper<OauthClientDetails>().lambda()
                .eq(OauthClientDetails::getClientId, oauthClientDetailsDto.getClientId());
        return JsonResult.toJsonResult(oauthClientDetailsMapper.update(oauthClientDetailsDto, updateWrapper));
    }

    @Override
    public JsonResult deleteById(String clientId) {
        return JsonResult.toJsonResult(oauthClientDetailsMapper.deleteById(clientId));
    }

    @Override
    public JsonResult save(OauthClientDetailsDto oauthClientDetailsDto) {
        return JsonResult.toJsonResult(oauthClientDetailsMapper.insert(oauthClientDetailsDto));
    }

    @Override
    public JsonResult findByClientId(String clientId) {
        OauthClientDetails oauthClientDetails = oauthClientDetailsMapper.selectById(clientId);
        return JsonResult.success(BeanCovert.INSTANCE.toConvertDTO(oauthClientDetails));
    }
}
